package com.cskaoyan.service;

import com.cskaoyan.bean.Admin;
import com.cskaoyan.bean.AdminExample;
import com.cskaoyan.bean.Role;
import com.cskaoyan.bo.AuthloginBo;
import com.cskaoyan.mapper.AdminMapper;
import com.cskaoyan.mapper.PermissionMapper;
import com.cskaoyan.mapper.RoleMapper;
import com.cskaoyan.utils.PermissionApiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TreeSet;


@Service
public class AuthServicelmpl implements AuthService {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    PermissionMapper permissionMapper;

    /**
     * 功能:登入,并将登录ip及时间插入数据库
     * 作者:whj
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
    @Override
    public int query(AuthloginBo authloginBo) {
        Admin admin = new Admin();
        admin.setUsername(authloginBo.getUsername());
        admin.setPassword(authloginBo.getPassword());
        Admin result = adminMapper.selectAdminByUsernameAndPassword(admin);
        if (result == null) {
            return 404;
        }
        //加入登录ip及时间信息
        String hostAddress = null;
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        if (hostAddress != null) {
            result.setLastLoginIp(hostAddress);
        } else {
            result.setLastLoginIp("0.0.0.0");
        }
        result.setLastLoginTime(new Date(System.currentTimeMillis()));
        int code = adminMapper.updateByPrimaryKey(result);
        if (code == 0) {
            return 404;
        }
        return 200;
    }

    @Override
    public Admin query(String username) {
        Admin admin = new Admin();
        admin.setUsername(username);
        return adminMapper.selectAdminByUsername(admin);
    }

    @Override
    public String queryRoleName(Integer roleId) {
        Role role = roleMapper.selectByPrimaryKey(roleId);
        return role.getName();
    }

    @Override
    public ArrayList<String> queryPermsList(String username) {

        Admin admin = new Admin();
        admin.setUsername(username);
        Admin realAdmin = adminMapper.selectAdminByUsername(admin);

        //拿到角色id
        Integer[] roleIds = realAdmin.getRoleIds();

        TreeSet<String> strings = new TreeSet<>();
        for (Integer roleId : roleIds) {

            List<Integer> permission = permissionMapper.selectRolePermission(roleId);

            for (Integer permissionId : permission) {
                String api = permissionMapper.selectPermissionAPIById(permissionId);
                strings.add(api);
            }
        }
        return new ArrayList<>(strings);
    }
}
